<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Information;
use App\Models\Nav;
use Illuminate\Http\Request;

class InformationController extends Controller
{
    public $nav = [];

    public function __construct()
    {
        $this->nav = Nav::orderBy('order', 'asc')->get()->toArray();
    }

    public function index($id)
    {
        //根据id获取分类
        $category = Category::findOrFail($id)->toArray();

        $left_title = $right_title = $category['title'];
        $site_map[] = $category;

        if ($category['parent_id'] > 0) {
            $parent_cats = Category::getParentCat($category['parent_id']);
            $site_map = array_reverse($parent_cats);
            array_push($site_map, $category);
        }

        //获取子分类
        $sub_categories = Category::where('parent_id', $id)->get()->toArray();

        $data = [];

        //判断是否有子分类
        if ($sub_categories) {
            //有子分类

            $sub_ids = [];
            foreach ($sub_categories as $key=>$val) {
                $data[$val['id']] = $val;
                $sub_ids[] = $val['id'];
            }

            $informations = Information::whereIn('gov_informations.cat_id', $sub_ids)
                ->whereRaw('5 > (SELECT COUNT(*) FROM gov_informations as a WHERE a.cat_id=gov_informations.cat_id AND a.created_at>gov_informations.created_at)')
                ->orderBy('gov_informations.cat_id', 'desc')
                ->orderBy('gov_informations.created_at', 'desc')
                ->get()->toArray();

            foreach ($informations as $key=>$val) {
                if (isset($data[$val['cat_id']]))
                    $data[$val['cat_id']]['informations'][] = $val;
            }

        } else {
            //没有子分类
            $parent_id = 0;
            if ($category['parent_id'] > 0 && isset($parent_cats)) {
                $parent = current($parent_cats);
                $parent_id = $parent['id'];
                $left_title = $parent['title'];
            }

            $brother_categories = Category::where('parent_id', $parent_id)->get()->toArray();

            foreach ($brother_categories as $key=>$val) {
                if ($val['id'] == $id)
                    $brother_categories[$key]['curr'] = 1;
                else
                    $brother_categories[$key]['curr'] = 0;
            }

            $data['categories'] = $brother_categories;
            $data['informations'] = Information::where('cat_id', $id)->paginate(18)->toArray();
        }

        $data = compact('left_title', 'right_title', 'site_map', 'data', 'id');

        $data['nav'] = $this->nav;

        return view('http.information', $data);
    }

    public function search(Request $request)
    {
        $cat_id   = $request->get('cat_id', 0);
        $keywords = $request->get('keywords', '');
        $type     = $request->get('type', 0);
        $sort     = $request->get('sort', 1);

        $categories = Information::where('gov_informations.title','like','%'.$keywords.'%')
            ->leftJoin('gov_categories as b', 'b.id', 'gov_informations.cat_id')
            ->selectRaw('gov_informations.cat_id,b.title,count(*) count')
            ->groupBy('gov_informations.cat_id')
            ->get()->toArray();

        $categories = collect($categories)->prepend(['cat_id' => 0, 'title' => '全部', 'count' => array_sum(array_column($categories, 'count'))])->toArray();

        $informations = Information::where('gov_informations.title','like','%'.$keywords.'%')
            ->when($cat_id, function ($query) use ($cat_id){
                $query->where('gov_informations.cat_id', $cat_id);
            })
            ->when($type, function ($query) use ($type){
                $date = get_time($type);
                $query->where('gov_informations.created_at', '>=', $date['start_time'])->where('gov_informations.created_at', '<=', $date['end_time']);
            })
            ->when($sort, function ($query) use ($sort){
                $sort = $sort == 1 ? 'asc' : 'desc';
                $query->orderBy('gov_informations.created_at', $sort);
            })
            ->selectRaw('gov_informations.id,gov_informations.title,gov_informations.summary,gov_informations.created_at,b.title cat_name')
            ->leftJoin('gov_categories as b', 'b.id', 'gov_informations.cat_id')
            ->paginate(10)->toArray();

        $data = compact('keywords','categories', 'informations', 'cat_id', 'type', 'sort');

        $data['nav'] = $this->nav;

        return view('http.search', $data);

    }

    public function detail($id)
    {
        $information = Information::where('gov_informations.id', $id)
            ->leftJoin('admin_users as b','b.id','gov_informations.user_id')
            ->select('gov_informations.*','b.username')
            ->first();

        if ($information) {
            $information->increment('read_count', 1);
            $information->save();

            $information = $information->toArray();
            $site_map = Category::getParentCat($information['cat_id']);
            $site_map = array_reverse($site_map);

            $data = compact('site_map', 'information');
            $data['nav'] = $this->nav;
            return view('http.detail', $data);
        } else {
            abort(404);
        }
    }
}